package com.xz.sales.util;

import cn.hutool.core.date.DateUtil;
import com.xz.patient.domain.PatientInfo;
import com.xz.sales.domain.SalesOrderDetail;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class ProcessOrderUtil {
    /**
     * 计算订单状态
     * @param salesOrderDetails 订单详情集合
     * @return 订单状态
     */
    public static Integer processOrderStatusByDetails(List<SalesOrderDetail> salesOrderDetails){
        List<Integer> detailStatusList = salesOrderDetails.stream().map(SalesOrderDetail::getProductStatus).distinct().collect(Collectors.toList());
        return processOrderStatusByStatus(detailStatusList);
    }
    /**
     * 计算订单状态
     * @param detailStatusList 订单详情状态集合
     * @return 订单状态
     */
    public static Integer processOrderStatusByStatus(List<Integer> detailStatusList){

        // 这一大串大致内容是，利用订单详情的状态，与订单状态的对应关系。
        // 通过交集求出订单状态

        List<Integer> waitPay = new ArrayList<>(Arrays.asList(1));
        waitPay.retainAll(detailStatusList);
        if(waitPay.size()>0){
            return 1;
        }
        List<Integer> salesProcessing = new ArrayList<>(Arrays.asList(2, 3));
        salesProcessing.retainAll(detailStatusList);
        if(salesProcessing.size()>0){
            return 2;
        }
        List<Integer> unDelivered = new ArrayList<>(Arrays.asList(4,5,6,7,8,9,10,11));
        unDelivered.retainAll(detailStatusList);
        if(unDelivered.size()>0){
            return 3;
        }
        List<Integer> salesService = new ArrayList<>(Arrays.asList(13,14,15));
        salesService.retainAll(detailStatusList);
        if(salesService.size()>0){
            return 5;
        }
        List<Integer> afterSales = new ArrayList<>(Arrays.asList(16,17,18));
        afterSales.retainAll(detailStatusList);
        if(afterSales.size()>0){
            return 6;
        }
        List<Integer> delivered = new ArrayList<>(Arrays.asList(12));
        delivered.retainAll(detailStatusList);
        if(delivered.size()>0){
            return 4;
        }
        return 0;
    }

    /**
     * 计算会员名称
     * @param patientInfo
     * @return
     */
    public static String processMemberName(PatientInfo patientInfo) {
        StringBuilder memberName = new StringBuilder();
        if(Objects.nonNull(patientInfo)){
            String sex = "";
            Integer sexNum = patientInfo.getSex();
            if(sexNum == null || sexNum == 0){
                sex = "-";
            }else if(sexNum == 1){
                sex = "男";
            }else if(sexNum == 2){
                sex = "女";
            }
            int year = DateUtil.ageOfNow(patientInfo.getBirthday());
            memberName.append(patientInfo.getPatientName()).append("/").append(sex).append("/").append(year).append("岁");
        }
        return memberName.toString();
    }
}
